package cn.zmit.kuxi.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;

import com.lidroid.xutils.util.LogUtils;
import com.xdroid.common.utils.ToastUtils;

/**
 * 公告工具类
 * 
 * @author Robin time 2015-03-06 16:16:25
 * 
 */
public class CommonUtils {
	/**
	 * 横向显示动画
	 */
	public static void showView(View view) {

		TranslateAnimation mShowAction = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		mShowAction.setDuration(500);

		view.startAnimation(mShowAction);
		view.setVisibility(View.VISIBLE);
	}

	/**
	 * 横向隐藏动画
	 * 
	 * @param view
	 */
	public static void hideView(View view) {

		TranslateAnimation mHiddenAction = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		mHiddenAction.setDuration(500);
		view.startAnimation(mHiddenAction);
		view.setVisibility(View.GONE);
	}

	/**
	 * 垂直方向展开动画(重载函数)
	 * @param view
	 *            要展开的视图
	 */
	public static void expandingAnimation(final View view) {

	       final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
	        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
	        view.measure(widthSpec, heightSpec);  
	        expandingAnimation(view, view.getMeasuredHeight());
	}
	
    /**
	 * 垂直方向展开动画
	 * @param view  要展开的视图
	 * @param viewHeight
	 *            展开的视图的高
	 */
	public static void expandingAnimation(final View view, int viewHeight) {  
        view.setVisibility(View.VISIBLE);  
       ValueAnimator animator = createHeightAnimator(view, 0, viewHeight);  
        animator.start();  
    }  

	/**
	 * 垂直方向折叠动画
	 * 
	 * @param view
	 *            要折叠的视图
	 */
	public static void collapsingAnimation(final View view) {
		int origHeight = view.getHeight();
		LogUtils.i("TopContent高:" + origHeight);

		ValueAnimator animator = createHeightAnimator(view, origHeight, 0);
		animator.addListener(new AnimatorListenerAdapter() {
			public void onAnimationEnd(Animator animation) {
				view.setVisibility(View.GONE);
			};
		});
		animator.start();
	}

	/**
	 * 创建Animator对象
	 * 
	 * @param view
	 *            视图
	 * @param start
	 *            开始
	 * @param end
	 *            结束
	 * @return Animator对象
	 */
	public static ValueAnimator createHeightAnimator(final View view,
			int start, int end) {
		ValueAnimator animator = ValueAnimator.ofInt(start, end);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator valueAnimator) {
				int value = (Integer) valueAnimator.getAnimatedValue();

				ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
				layoutParams.height = value;
				view.setLayoutParams(layoutParams);
			}
		});
		// animator.setDuration(DURATION);
		return animator;
	}
	
    /**
     * 显示自定义Toast
     * @param context 上下文
     * @param messageText 显示的文字消息
     */
//    public static void showCustomToast(Context context,String messageText){
//    	View v=View.inflate(context, R.layout.view_toast, null);
//    	TextView mMessage=(TextView) v.findViewById(R.id.tv_message);
//    	mMessage.setText(messageText);
//    	ToastUtils.showCustomToast(context, v,Toast.LENGTH_SHORT);
//    }
    
    /**
     * 显示自定义Toast
     * @param context 上下文
     * @param messageText 显示的文字消息
     */
//    public static void showCustomToast(Context context,String messageText,int duration){
//    	View v=View.inflate(context, R.layout.view_toast, null);
//    	TextView mMessage=(TextView) v.findViewById(R.id.tv_message);
//    	mMessage.setText(messageText);
//    	ToastUtils.showCustomToast(context, v,duration);
//    }
    
	/**
	 * 将Drawable转化为Bitmap
	 * @param drawable 要转换的Drawable
	 * @param width 转化后的宽度，若设置为0，则自动获取Drawable宽度
	 * @param height 转化后的高度，若设置为0，则自动获取Drawable高度
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable,int width,int height) {
		if (width==0) {
			width = drawable.getIntrinsicWidth();
		}
		
		if (height==0) {
			height = drawable.getIntrinsicHeight();
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
				.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;

	}
	
	/**
	 * 加中划线
	 * @param textView
	 */
	public static void addLineThrough(TextView textView){
		textView.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  //设置中划线加清晰
	}
	
}
